﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace SharpSoft.WPF.D3D
{
    public static class D3DHelper
    {

        ///// <summary>
        ///// 将2D路径转为3D模型
        ///// </summary>
        ///// <param name="pathGeometry">路径</param>
        ///// <param name="slicesCount">切片（采样）数量</param>
        ///// <param name="startPoint">开始点（指定2D桌面坐标系中的原点0,0对应到3D世界坐标中的起点）</param>
        ///// <param name="rightVector">2D-X轴正方向对应到3D世界的方向</param>
        ///// <param name="upVector">2D-Y轴负方向对应到3D世界的方向</param>
        ///// <param name="backVector">指定形成3D模型背面的方向，该参数决定了最终生成模型的厚度与倾斜度。</param>
        ///// <returns></returns>
        //public static MeshGeometry3D CreateMeshGeometry3D(Geometry geometry, double tolerance, Vector3D startPoint, Rotation3D rotation3D, double scale, Vector3D backVector)
        //{
        //    if (slicesCount < 2)
        //    {
        //        throw new ArgumentException("slicesCount", "采样数量必须大于等于2.");
        //    }

        //    var pg = geometry.GetFlattenedPathGeometry(tolerance, ToleranceType.Relative);
             
        //    List<Point> list = new List<Point>();
        //    foreach (PathFigure fig in pg.Figures)
        //    {
        //        list.Clear();
        //        list.Add(fig.StartPoint);

        //        foreach (PathSegment seg in fig.Segments)
        //        {
        //            if (seg is LineSegment)
        //            {
        //                LineSegment lineseg = seg as LineSegment;
        //                list.Add(lineseg.Point);
        //            }

        //            else if (seg is PolyLineSegment)
        //            {
        //                PolyLineSegment polyline = seg as PolyLineSegment;

        //                for (int i = 0; i < polyline.Points.Count; i++)
        //                    list.Add(polyline.Points[i]);
        //            }
        //        }

        //        // Figure is complete. Post-processing follows. 
        //        if (list.Count > 0)
        //        {
        //            // Remove last point if it's the same as the first. 
        //            if (list[0] == list[list.Count - 1])
        //                list.RemoveAt(list.Count - 1);

        //            // Convert points to Y increasing up. 
        //            for (int i = 0; i < list.Count; i++)
        //            {
        //                Point pt = list[i];
        //                pt.Y = 2 * startPoint.Y - pt.Y;
        //                list[i] = pt;
        //            }

        //            // For each figure, process the points. 
        //            ProcessFigure(list, vertices, normals, indices, textures);
        //        }
        //    }






        //    Transform3DGroup t3d = new Transform3DGroup();
        //    t3d.Children.Add(new TranslateTransform3D(startPoint));//平移
        //    t3d.Children.Add(new RotateTransform3D(rotation3D));//旋转
        //    t3d.Children.Add(new ScaleTransform3D(new Vector3D(scale, scale, scale)));//缩放
        //    for (int i = 0; i < points.Length; i++)
        //    {
        //        points[i] = t3d.Transform(points[i]);
        //    }
        //    var trasl = new TranslateTransform3D(backVector);//用于平移生成背部点
        //    var backpoints = new Point3D[points.Length];
        //    for (int i = 0; i < points.Length; i++)
        //    {
        //        backpoints[i] = trasl.Transform(points[i]);
        //    }

        //    MeshGeometry3D m3d = new MeshGeometry3D();
        //    Point3DCollection p3c = new Point3DCollection(points.Length * 2);
        //    for (int i = 0; i < points.Length; i++)
        //    {
        //        p3c.Add(points[i]);
        //    }
        //    for (int i = 0; i < backpoints.Length; i++)
        //    {
        //        p3c.Add(backpoints[i]);
        //    }
        //    m3d.Positions = p3c;

        //    Int32Collection int32c = new Int32Collection();
        //    for (int i = 0; i < points.Length / 2; i++)
        //    {
        //        int32c.Add(i);
        //        int32c.Add(points.Length - 1 - i);
        //        int32c.Add(i + 1);

        //        int32c.Add(i + 1);
        //        int32c.Add(points.Length - 1 - i);
        //        int32c.Add(points.Length - 2 - i);
        //    }
        //    for (int i = 0; i < backpoints.Length / 2; i++)
        //    {
        //        var j = i + points.Length;
        //        int32c.Add(j);
        //        int32c.Add(points.Length - 1 - j);
        //        int32c.Add(j + 1);

        //        int32c.Add(j + 1);
        //        int32c.Add(points.Length - 1 - j);
        //        int32c.Add(points.Length - 2 - j);
        //    }
        //    m3d.TriangleIndices = int32c;

        //    m3d.TextureCoordinates.Add(new Point(0, 0));
        //    m3d.TextureCoordinates.Add(new Point(1, 0));
        //    m3d.TextureCoordinates.Add(new Point(1, 1));
        //    m3d.TextureCoordinates.Add(new Point(0, 1));

        //    return m3d;

        //}

        //public static MeshGeometry3D[] CreateText3D(FormattedText text, int slicesCount, Vector3D startPoint, Rotation3D rotation3D, double scale, Vector3D backVector)
        //{
        //    var g = text.BuildGeometry(new Point());
        //    List<MeshGeometry3D> list = new List<MeshGeometry3D>();
        //    procGeometry(ref list, g, slicesCount, startPoint, rotation3D, scale, backVector);

        //    return list.ToArray();
        //}

        //static void procGeometry(ref List<MeshGeometry3D> list, Geometry g, int slicesCount, Vector3D startPoint, Rotation3D rotation3D, double scale, Vector3D backVector)
        //{
        //    if (g is GeometryGroup gg)
        //    {
        //        foreach (var item in gg.Children)
        //        {
        //            procGeometry(ref list, item, slicesCount, startPoint, rotation3D, scale, backVector);
        //        }
        //    }
        //    else if (g is PathGeometry pg)
        //    {
        //        list.Add(CreateMeshGeometry3D(pg, slicesCount, startPoint, rotation3D, scale, backVector));
        //    }
        //    else
        //    {
        //        throw new Exception("不能处理");
        //    }
        //}
    }
}
